const Koa = require('koa2')
const app = new Koa()
const Router = require('koa-router')
const  bodyParser = require('koa-bodyparser');
let router = new Router()
const main = Context => {
  let {userName,password}=Context.request.body
  if(userName==='admin'&&password==='111111'){
    return Context.body={
      data:{
        token:'57af5b10-3a76-11e5-922a-75f42afeee38',
        name:'zhangmin',
        userName,
      },
      state:'S0001',

      message:'登录成功'
    }
  }else if(userName!=='admin'){
    return Context.body={
      data:{},
      state:'A0001',
      message:'用户名不正确'
    }
  }else if(password!=='111111'){
    return Context.body={
      data:{},
      state:'A0001',
      message:'密码不正确'
    }
  }

};
// router.post('/login', main)
app.use(bodyParser());
router.post('/login', main)

app.use(router.routes())
app.use(router.allowedMethods())

app.listen(3000, () => {
  console.log('[demo] route-use-middleware is starting at port 3000')
})
